import React from 'react';
import { useNavigate } from 'react-router-dom';

import Home from '../MessageModule/Home';

interface UserData {
  username: string;
  image: string;
  isLoggedIn: boolean;
}

interface UserData {
  _id?: string;
  username: string;
  image: string;
  isLoggedIn: boolean;
}

// Home页面的包装组件，用于提供用户信息
const HomeWrapper: React.FC = () => {
  const navigate = useNavigate();
  const loginUsername = localStorage.getItem('login_username');
  const loginType = localStorage.getItem('login_type');

  if (!loginUsername || !loginType) {
    // 如果没有登录信息，重定向到登录页面
    navigate('/login');
    return null;
  }

  // 构造用户信息对象
  const userData: UserData = {
    username: loginUsername,
    image:
      localStorage.getItem('login_avatar') ||
      'https://img1.baidu.com/it/u=3217838212,795208401&fm=253&fmt=auto&app=138&f=JPEG?w=514&h=500',
    isLoggedIn: true,
  };

  return <Home currentUser={userData} />;
};

export default HomeWrapper;
